package org.jdcenter.rmi.impl;

import java.rmi.RemoteException;
import java.util.logging.Logger;

import org.jdcenter.rmi.abs.*;

public class PeerChannel implements Channel {
    private static Logger aLogger = Logger.getLogger(PeerChannel.class.getCanonicalName());
    private Logger getLogger() {
        return aLogger;
    }

    private RpcArgument argument;
    private RemoteChannel channel;

    public PeerChannel(RemoteChannel channel) {
        this.channel = channel;
        this.argument = new RpcArgument();
    }

    public Object invoke(String method,Object[] args,Object result) throws ChannelException, HandlerException {
        try {
            getLogger().finest("PeerChannel is invoked with argument " + args.toString());
            return channel.invoke(argument.getCookie(),method,args);
        } catch (RemoteException e) {
            getLogger().severe(Thrower.toString(e));
            throw new ChannelException(e);
        } catch (HandlerInvalidException e) {
            // Never should happen
            getLogger().severe(Thrower.toString(e));
            throw new ChannelException(e);
        } catch (HandlerRejectedException e) {
            // Never should happen
            getLogger().severe(Thrower.toString(e));
            throw new ChannelException(e);
        }
    }
}

